package com.example.app.manager

import androidx.multidex.BuildConfig
import com.blankj.utilcode.constant.TimeConstants
import com.blankj.utilcode.util.LogUtils
import java.util.Timer
import java.util.TimerTask


/**
 * CreateBy:Joker
 * CreateTime:2024/2/5 11:10
 * description：定时任务工具
 */
object TimerTaskManager {

    private const val TAG = "TimerTaskManager"
    private const val HEART_BEAT_TIME = 3L * TimeConstants.SEC //心跳时间间隔

    private var timerHeartBeat: Timer? = null
    private var timerHeartBeatTask: TimerTask? = null

    fun startTimer() {
        LogUtils.iTag(TAG, "startTimer")

        if (timerHeartBeat == null) timerHeartBeat = Timer()
        if (timerHeartBeatTask == null) {
            timerHeartBeatTask = object : TimerTask() {
                override fun run() {
                    if (BuildConfig.DEBUG) LogUtils.dTag(TAG, "timerHeartBeatTask")

                }
            }

            timerHeartBeat?.schedule(timerHeartBeatTask, HEART_BEAT_TIME, HEART_BEAT_TIME)
        }
    }

    fun cancelTimer() {
        LogUtils.wTag(TAG, "cancelTimer")
        timerHeartBeat?.cancel()
        timerHeartBeat?.purge()
        timerHeartBeat = null

        timerHeartBeatTask?.cancel()
        timerHeartBeatTask = null
    }
}